<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MallUnionGoods extends BaseModel
{
    //
    protected $table = 'mall_union_goods';
    protected $connection = 'api';
    protected $primaryKey = 'm_u_g_id';

    public function getlist(Request $request){
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $union = $request->input('union_id',0);
        $goods = $request->input('goods_id',0);
        $status = $request->input('status',0);
        $query = DB::connection($this->connection)->table($this->table);
        if ($union) $query->where('union_id',$union);
        if ($goods) $query->where('goods_id',$goods);
        if (!empty($status)) $query->where('status',$status);

        $data = $query->paginate($limit,'*','page',$page);
        if (!$data->toArray()['data']){
            return $data;
        }
        $union_ids = array_unique(array_column($data->toArray()['data'],'union_id'));

        $goods_ids = array_unique(array_column($data->toArray()['data'],'good_id'));

        $unionData = (new Union())->getUnionByUnIds($union_ids);
        $unionData = array_column($unionData,'name','id');
        $mallGoodsData = (new MallGoods())->getGoodsBygoodIds($goods_ids);
        $mallGoodsData = array_column($mallGoodsData,null,'goods_id');
        foreach ($data as $res){
            $res->goods_name = $mallGoodsData[$res->good_id]->goods_name??'';
            $res->goods_type = $mallGoodsData[$res->good_id]->goods_type??'';
            $res->small_img = $mallGoodsData[$res->good_id]->small_img??'';
            $res->union_name = $unionData[$res->union_id]??'';
        }
        return $data;


    }
    public function ungiftCreate(Request $request){
        $union_id = $request->input('union_id');
        $good_id = $request->input('good_id');
        $status = $request->input('status');
        $data = [
            'union_id' => $union_id,
            'good_id' => $good_id,
            'status' => $status
        ];
        $url = getKkaudioApiUrl();
        return curl_request($url . '/inner/mall/union/create', $data, 'json', 'POST');


    }

    public function ungiftUpdate(Request $request){
        $union_id = $request->input('union_id');
        $good_id = $request->input('good_id');
        $status = $request->input('status');
        $id = $request->input('id');
        $data = [
            'id' => $id,
            'union_id' => $union_id,
            'good_id' => $good_id,
            'status' => $status
        ];
        $url = getKkaudioApiUrl();
        return curl_request($url . '/inner/mall/union/edit', $data, 'json', 'POST');
    }

    public function ungiftBackLog(Request $request){
        $union = $request->input('union_id');
        $good_id = $request->input('goods_id');

        $user = DB::connection($this->connection)->table('union_signing')->where('un_id',$union)->get(['user_id'])->toArray();
        $user = array_column($user,'user_id');
        $backlog = DB::connection($this->connection)->table('mall_user_goods')
            ->where('end_timestamp','>',time())
            ->whereIn('user_id',$user)
            ->where('goods_id',$good_id)
            ->get(['user_id'])->toArray();
        $backlog = array_column($backlog,'user_id');
        return $backlog;
    }
}
